function getScroll(){
    return {
        left: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0,
        top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0
    };
}
Element.prototype.hide = function () {
    this.style.display = 'none'
}
Element.prototype.showLoading = function () {
    if(!this.querySelector('.loading')) {
        let loadingDom = document.createElement('div')
        loadingDom.className = 'loading'
        let iconDom = document.createElement('i')
        iconDom.className = 'iconfont icongengxin'
        loadingDom.appendChild(iconDom)
        if(this.tagName === 'BODY') {
            // 获取滚动条高度
            loadingDom.style.top = getScroll().top + 'px'
            hideScroll()
        } else {
            loadingDom.style.height = 'auto'
            loadingDom.style.bottom = '0'
        }
        loadingDom.style.zIndex = '999'
        this.appendChild(loadingDom)
    }
}
Element.prototype.hideLoading = function () {
    let e = this.querySelector('.loading')
    if(e) {
        e.remove()
        if(this.tagName === 'BODY') {
            showScroll()
        }
    }

}
Element.prototype.show = function (display = 'block') {
    this.style.display = display
}
Element.prototype.toggleShow = function (display = 'block') {
    if (this.style.display === display) {
        this.hide()
    } else {
        this.style.display = display
    }
}
function showLoading (el= document.body) {
    if(el) {
        el.showLoading()
    }
}
function hideLoading(el = document.body) {
    el.hideLoading()
}
function showToast(val, time = 1500) {
    let toastDom = document.createElement('div')
    toastDom.innerHTML = val
    toastDom.className = 'toast'
    if(val.length < 40) {
        toastDom.style.whiteSpace = 'nowrap'
    }
    toastDom.style.top = getScroll().top + 200 + 'px'
    toastDom.style.zIndex = '9999'
    document.body.appendChild(toastDom)
    setTimeout(() => {
        document.querySelector('.toast').remove()
    }, 1500)
}
function hideScroll(el = document.body) {
    el.style.overflowY = 'hidden'
}
function showScroll(el = document.body) {
    el.style.overflowY = 'auto'
}
function showMask({zIndex,event} = {}) {
    if(document.querySelector('body>.mask')) {
        return
    }
    let mask = document.createElement('div')
    mask.className = 'mask'
    if(zIndex) {
        mask.style.zIndex = zIndex
    }
    mask.style.top = getScroll().top  + 'px'
    document.body.appendChild(mask)
    mask.show()
    if(event) {
        mask.addEventListener('click', event)
    } else {
        mask.addEventListener('click', hideMask)
    }
    document.body.style.overflowY = 'hidden'
}
function hideMask() {
    let e = document.querySelector('body>.mask')
    if(e) {
        e.hide()
        e.remove()
    } else {
        console.warn('not find mask')
    }

    document.body.style.overflowY = 'auto'
}

function showModal(config = {}) {
    if(document.querySelector('.modal')){
        return
    }
    let defaultConfig = {
        class: '',
        content: 'body',
        showTop: true,
        isShowConfirmBtn: true,
        confirmBtnText: 'confirm',
        confirmBtn: {
            event: closeModal
        },
        isShowCancelBtn: true,
        cancelBtnText: 'cancel',
        cancelBtn: {
            event: closeModal
        }
    }
    showMask({
        zIndex: 450,
        event: closeModal
    })
    Object.assign(defaultConfig, config)
    let modal = document.createElement('div')
    modal.className = 'modal ' + defaultConfig.class.trim()
    modal.style.top = getScroll().top + 200 + 'px'
    // header
    if(defaultConfig.showTop) {
        let header = document.createElement('div')
        header.className = 'modal-header'
        let closeIcon = document.createElement('i')
        closeIcon.className = 'iconfont iconbuzhichi'
        closeIcon.addEventListener('click', closeModal)
        header.appendChild(closeIcon)
        modal.appendChild(header)
    }
    // body
    let body = document.createElement('div')
    body.className = 'modal-body'
    body.innerHTML = defaultConfig.content
    modal.appendChild(body)
    // footer
    if(defaultConfig.isShowConfirmBtn || defaultConfig.isShowCancelBtn) {
        let footer = document.createElement('div')
        footer.className = 'modal-footer'
        if(defaultConfig.isShowConfirmBtn) {
            let confirmBtn = document.createElement('button')
            confirmBtn.className = 'btn btn-info'
            confirmBtn.innerText = defaultConfig.confirmBtnText
            confirmBtn.addEventListener('click', defaultConfig.confirmBtn.event)
            footer.appendChild(confirmBtn)
        }
        if(defaultConfig.isShowCancelBtn) {
            let cancelBtn = document.createElement('button')
            cancelBtn.className = 'btn btn-default'
            cancelBtn.innerText = defaultConfig.cancelBtnText
            cancelBtn.addEventListener('click', defaultConfig.cancelBtn.event)
            footer.appendChild(cancelBtn)
        }
        modal.appendChild(footer)
    }
    document.body.appendChild(modal)
}
function closeModal () {
    document.querySelector('.modal').remove()
    hideMask()
}
function showNoData(el, minHeight) {
    let e = document.createElement('div')
    e.className = 'no-data'
    let img = document.createElement('i')
    img.className = 'iconfont iconkongshuju2'
    e.appendChild(img)
    let tx = document.createElement('div')
    tx.innerText = 'No related data available'
    e.appendChild(tx)
    el.appendChild(e)
    if(minHeight) {
        el.parentNode.style.minHeight = minHeight + 'px'
    }
}
function showTips(e) {
    e.children[0].show()
}
function hideTips(e) {
    e.children[0].hide()
}
function getEncodeUrl(url, data = {}) {
    return encodeURI(url + '?data=' + JSON.stringify(data))
}
function checkEmail(str) {
    let reg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/
    return reg.test(str)
}

